package com.md.notes.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.md.notes.R;
import com.md.notes.fragment.EditFragment;
import com.md.notes.fragment.ViewFragment;
import com.md.notes.model.entity.Note;

public class WriteActivity extends AppCompatActivity {
    private AppCompatActivity activity;
    private Intent preIntent;
    private RadioGroup bottomNavigation;
    private RadioButton radioView;
    private RadioButton radioEdit;

    private ViewFragment viewFragment;
    private EditFragment editFragment;
    private Fragment initFrag;
    private FragmentManager fragmentManager;

    private String content;
    public enum FragmentType {
        VIEW,   // 查看模式
        EDIT    // 编辑模式
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_write);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initView();
        initData();
        addListener();
    }

    private void initView() {
        activity = WriteActivity.this;
        bottomNavigation = (RadioGroup)findViewById(R.id.bottom_navigation);
        radioView = (RadioButton) findViewById(R.id.radio_view);
        radioEdit = (RadioButton) findViewById(R.id.radio_edit);
    }

    private void initData() {
        // 获取Intent
        preIntent = getIntent();
        content = preIntent.getStringExtra("content");
        // 初始化FragmentManager
        fragmentManager = getSupportFragmentManager();
        viewFragment = ViewFragment.newInstance(content,"");
        editFragment = EditFragment.newInstance("","");

        // 将字符串转换回枚举，默认为EDIT模式
        String typeStr = preIntent.getStringExtra("type");
        FragmentType currentType = typeStr != null ? FragmentType.valueOf(typeStr) : FragmentType.EDIT;

        // 根据类型切换Fragment
        switch (currentType) {
            case VIEW:
                initFrag = viewFragment;
                radioView.performClick();
                break;
            case EDIT:
            default:
                initFrag = editFragment;
                radioEdit.performClick();
                break;
        }

        switchFragment(initFrag);
    }

    private void addListener() {
        bottomNavigation.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.radio_view) {
                    String editContent = editFragment.getEditContent();
                    Bundle args = new Bundle();
                    args.putString(ViewFragment.ARG_PARAM1, editContent);
                    viewFragment.setArguments(args);
                    switchFragment(viewFragment);
                }
                if (checkedId == R.id.radio_edit) {
                    Bundle args = new Bundle();
                    args.putString(EditFragment.ARG_PARAM1, content);
                    editFragment.setArguments(args);
                    switchFragment(editFragment);
                }
            }
        });
    }

    private void switchFragment(Fragment fragment) {
        // 使用FragmentManager进行Fragment事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_container, fragment); // 假设有一个ID为fragment_container的容器
        transaction.commit();
    }

    public static void startUI(FragmentActivity activity) {
        Intent intent = new Intent(activity, WriteActivity.class);
        // 存储枚举的字符串表示
        intent.putExtra("type", FragmentType.EDIT.name());
        activity.startActivity(intent);
    }
    public static void startUI2View(FragmentActivity activity, Note note) {
        Intent intent = new Intent(activity, WriteActivity.class);
        // 存储枚举的字符串表示
        intent.putExtra("type", FragmentType.VIEW.name());
        intent.putExtra("content",note.getContent());
        activity.startActivity(intent);
    }
}